package com.filebackup.struts.actions;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import com.filebackup.business.LocalFileService;
import com.filebackup.business.LocalFolderService;
import com.filebackup.daemon.DaemonBoss;
import com.filebackup.daemon.DaemonConstants;
import com.filebackup.util.RootPathUtil;
import com.filebackup.vo.impl.LocalFileVo;
import com.filebackup.vo.impl.LocalFolderVo;
import com.filebackup.vo.impl.UserVo;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class UploadAction extends FilebackupSupport {

	private int id;
	private int type;
	private int upperId;
	private File file;
	private String contentType;
	private String fileName;
	private String method;
	private Integer newId;

	private LocalFileService localFileService;
	private LocalFolderService localFolderService;

	private final static int BUFFER_SIZE = 1024 * 1024;

	public void setUpload(File file) {
		this.file = file;
	}

	/*
	 * public File getUpload() { return this.file; }
	 */

	public void setUploadContentType(String contentType) {
		this.contentType = contentType;
	}

	/**
	 * public String getUploadContentType() { return this.contentType; }
	 */

	public void setUploadFileName(String filename) {
		this.fileName = filename;
	}

	public void setFileName(String filename) {
		
		this.fileName = filename;
	}

	public String execute() throws Exception {
		// ...
		this.getRequest().setCharacterEncoding("utf-8");
		this.setFileName(this.getRequest().getParameter("fileName"));
		System.out.println(file);
		System.out.println(fileName);

		String path = "";
		if ("modify".equals(this.getMethod())) {
			UserVo userVo=this.getLocalFileService().findOwnerById(id);
			path=RootPathUtil.ROOT_PATH + userVo.getName() + "\\"+this.getLocalFileService().returnPath(id);
			uploadFile(path);
			this.getLocalFileService().changeFile(id);
		} else if ("upload".equals(this.getMethod())) {
			fileName = new String(fileName.getBytes());
			UserVo userVo = null;
			if (upperId == 0)
				userVo = this.getSessionUser();
			else
				userVo = this.getLocalFolderService().findOwnerById(upperId);
			if (this.getType() == 1) {
				if (upperId == 0)
					path = RootPathUtil.ROOT_PATH + userVo.getName() + "\\"
							+ fileName;
				else
					path = RootPathUtil.ROOT_PATH + userVo.getName() + "\\"
							+ this.getLocalFolderService().returnPath(upperId)
							+ "\\" + fileName;
				System.out.println(path);
				File folder = new File(path);
				folder.mkdirs();
				LocalFolderVo vo = new LocalFolderVo();
				vo.setFolderName(fileName);
				vo.setOwnerId(userVo.getId());
				vo.setUpperId(upperId);
				vo.setVersion(1);
				this.getLocalFolderService().addFolder(vo);
				this.setNewId(this.getLocalFolderService().findNewFolder(vo));
			} else if (this.getType() == 2) {
				if (upperId == 0)
					path = RootPathUtil.ROOT_PATH + userVo.getName() + "\\"
							+ fileName;
				else
					path = RootPathUtil.ROOT_PATH + userVo.getName() + "\\"
							+ this.getLocalFolderService().returnPath(upperId)
							+ "\\" + fileName;
				uploadFile(path);
				LocalFileVo vo = new LocalFileVo();
				vo.setFileName(fileName);
				vo.setFolderId(upperId);
				vo.setOwnerId(userVo.getId());
				vo.setVersion(1);
				this.getLocalFileService().addFile(vo);
				this.getLocalFileService().findNewId(vo);
				this.setNewId(this.getLocalFileService().findNewId(vo));
			}
		}

		System.out.println("----------" + this.newId);

		// this.uploadFile("d:\\ddd\\");

		return SUCCESS;
	}

	public void uploadFile(String path) {

		try {

			System.out.println(file);
			System.out.println(fileName);

			FileOutputStream fileOutput = new FileOutputStream(path);

			FileInputStream fileInput = new FileInputStream(file);

			int count = 0;
			byte[] buffer = new byte[BUFFER_SIZE];
			do {

				count = fileInput.read(buffer, 0, BUFFER_SIZE);
				if (count > 0)
					fileOutput.write(buffer, 0, count);

			} while (count > 0);

			System.out.println(file);
			System.out.println(fileName);

			fileOutput.close();
			fileInput.close();

			/*
			 * DaemonBoss.getInstance().submitUploadJob(DaemonConstants.ALI_PATH,
			 * DaemonConstants.LOCAL_SAVEPATH + fileName,
			 * DaemonConstants.LOCAL_SAVEPATH + fileName);
			 */

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getId() {
		return id;
	}

	public void setType(int type) {
		this.type = type;
	}

	public int getType() {
		return type;
	}

	public void setUpperId(int upperId) {
		this.upperId = upperId;
	}

	public int getUpperId() {
		return upperId;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public String getMethod() {
		return method;
	}

	public void setLocalFileService(LocalFileService localFileService) {
		this.localFileService = localFileService;
	}

	public LocalFileService getLocalFileService() {
		return localFileService;
	}

	public void setLocalFolderService(LocalFolderService localFolderService) {
		this.localFolderService = localFolderService;
	}

	public LocalFolderService getLocalFolderService() {
		return localFolderService;
	}

	public void setNewId(Integer newId) {
		this.newId = newId;
	}

	public Integer getNewId() {
		return newId;
	}
}
